package servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import dao.AnnounceDAO;
import dao.AnnounceDaoJdbcImpl;
import dao.ClassNewsDAO;
import dao.ClassNewsDaoJdbcImpl;
import dao.HomeworkDAO;
import dao.HomeworkDaoJdbcImpl;
import modle.announce;
import modle.classnews;
import modle.homework;

/**
 * Servlet implementation class ClassNewsServlet
 */
@WebServlet("/ClassNewsServlet")
public class ClassNewsServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ClassNewsServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		System.out.println("::::::::::::::ClassNewsServlet:::::::::::");
		//获取信息
		JSONObject jsonObject = new JSONObject();
		//通知
		AnnounceDAO anD = new AnnounceDaoJdbcImpl();
		List<announce> announceList = new ArrayList<announce>();
		announceList = anD.readAllAnnounce();
		jsonObject.put("announce", announceList);
		//作业
		HomeworkDAO hwD = new HomeworkDaoJdbcImpl();
		List<homework> homeworkList = new ArrayList<homework>();
		homeworkList = hwD.readAllHomework();
		jsonObject.put("homework", homeworkList);
		//班级新闻
		ClassNewsDAO cnd = new ClassNewsDaoJdbcImpl();
		List<classnews> classNewsList = new ArrayList<classnews>();
		classNewsList = cnd.readAllClassNews();
		jsonObject.put("classNews", classNewsList);
		System.out.println("::::::::::::::end::ClassNewsServlet:::::::::::");
		response.setHeader("Content-type", "text/html;charset=UTF-8");
		response.setCharacterEncoding("UTF-8");
		
		String jsonStr = JSON.toJSONString(jsonObject);
		PrintWriter out = response.getWriter();
		out.println(jsonStr);
		out.close();
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
